home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 April / SGI IRIX 6.5 Applications 2004 April.iso / dist / mozilla.idb / var / netscape / mozilla / components / venkman-service.js.z / venkman-service.js
Text File  |  2004-01-06  |  16KB  |  561 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla Public License
  4.  * Version 1.1 (the "License"); you may not use this file except in
  5.  * compliance with the License. You may obtain a copy of the License at
  6.  * http://www.mozilla.org/MPL/ 
  7.  * 
  8.  * Software distributed under the License is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10.  * for the specific language governing rights and limitations under the
  11.  * License. 
  12.  *
  13.  * The Original Code is The JavaScript Debugger
  14.  * 
  15.  * The Initial Developer of the Original Code is
  16.  * Netscape Communications Corporation
  17.  * Portions created by Netscape are
  18.  * Copyright (C) 1998 Netscape Communications Corporation.
  19.  *
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU Public License (the "GPL"), in which case the
  22.  * provisions of the GPL are applicable instead of those above.
  23.  * If you wish to allow use of your version of this file only
  24.  * under the terms of the GPL and not to allow others to use your
  25.  * version of this file under the MPL, indicate your decision by
  26.  * deleting the provisions above and replace them with the notice
  27.  * and other provisions required by the GPL.  If you do not delete
  28.  * the provisions above, a recipient may use your version of this
  29.  * file under either the MPL or the GPL.
  30.  *
  31.  * Contributor(s):
  32.  *  Robert Ginda, <rginda@netscape.com>, original author
  33.  *
  34.  */
  35.  
  36. /* components defined in this file */
  37. const CLINE_SERVICE_CTRID =
  38.     "@mozilla.org/commandlinehandler/general-startup;1?type=venkman";
  39. const CLINE_SERVICE_CID =
  40.     Components.ID("{18269616-1dd2-11b2-afa8-b612439bda27}");
  41. const JSDPROT_HANDLER_CTRID =
  42.     "@mozilla.org/network/protocol;1?name=x-jsd";
  43. const JSDPROT_HANDLER_CID =
  44.     Components.ID("{12ec790d-304e-4525-89a9-3e723d489d14}");
  45. const JSDCNT_HANDLER_CTRID =
  46.     "@mozilla.org/uriloader/content-handler;1?type=x-application-jsd";
  47. const JSDCNT_HANDLER_CID =
  48.     Components.ID("{306670f0-47bb-466b-b53b-613235623bbd}");
  49.  
  50. /* components used by this file */
  51. const CATMAN_CTRID = "@mozilla.org/categorymanager;1";
  52. const STRING_STREAM_CTRID = "@mozilla.org/io/string-input-stream;1";
  53. const MEDIATOR_CTRID =
  54.     "@mozilla.org/appshell/window-mediator;1";
  55. const ASS_CONTRACTID =
  56.     "@mozilla.org/appshell/appShellService;1";
  57. const SIMPLEURI_CTRID = "@mozilla.org/network/simple-uri;1";
  58.  
  59. const nsIWindowMediator    = Components.interfaces.nsIWindowMediator;
  60. const nsIAppShellService   = Components.interfaces.nsIAppShellService;
  61. const nsICmdLineHandler    = Components.interfaces.nsICmdLineHandler;
  62. const nsICategoryManager   = Components.interfaces.nsICategoryManager;
  63. const nsIContentHandler    = Components.interfaces.nsIContentHandler;
  64. const nsIProtocolHandler   = Components.interfaces.nsIProtocolHandler;
  65. const nsIURI               = Components.interfaces.nsIURI;
  66. const nsIURL               = Components.interfaces.nsIURL;
  67. const nsIStringInputStream = Components.interfaces.nsIStringInputStream;
  68. const nsIChannel           = Components.interfaces.nsIChannel;
  69. const nsIRequest           = Components.interfaces.nsIRequest;
  70. const nsIProgressEventSink = Components.interfaces.nsIProgressEventSink;
  71. const nsISupports          = Components.interfaces.nsISupports;
  72.  
  73. function findDebuggerWindow ()
  74. {
  75.     var windowManager =
  76.         Components.classes[MEDIATOR_CTRID].getService(nsIWindowMediator);
  77.  
  78.     var window = windowManager.getMostRecentWindow("mozapp:venkman");
  79.  
  80.     return window;
  81. }
  82.  
  83. function safeHTML(str)
  84. {
  85.     function replaceChars(ch)
  86.     {
  87.         switch (ch)
  88.         {
  89.             case "<":
  90.                 return "<";
  91.             
  92.             case ">":
  93.                 return ">";
  94.                     
  95.             case "&":
  96.                 return "&";
  97.         }
  98.  
  99.         return "?";
  100.     };
  101.         
  102.     return String(str).replace(/[<>&]/g, replaceChars);
  103. }
  104.  
  105. /* Command Line handler service */
  106. function CLineService()
  107. {}
  108.  
  109. CLineService.prototype.commandLineArgument = "-venkman";
  110. CLineService.prototype.prefNameForStartup = "general.startup.venkman";
  111. CLineService.prototype.chromeUrlForTask = "chrome://venkman/content";
  112. CLineService.prototype.helpText = "Start with JavaScript Debugger.";
  113. CLineService.prototype.handlesArgs = false;
  114. CLineService.prototype.defaultArgs = "";
  115. CLineService.prototype.openWindowWithArgs = false;
  116.  
  117. /* factory for command line handler service (CLineService) */
  118. var CLineFactory = new Object();
  119.  
  120. CLineFactory.createInstance =
  121. function clf_create (outer, iid) {
  122.     if (outer != null)
  123.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  124.  
  125.     if (!iid.equals(nsICmdLineHandler) && !iid.equals(nsISupports))
  126.         throw Components.results.NS_ERROR_INVALID_ARG;
  127.  
  128.     return new CLineService();
  129. }
  130.  
  131. /* x-jsd: protocol handler */
  132.  
  133. const JSD_DEFAULT_PORT = 2206; /* Dana's apartment number. */
  134.  
  135. /* protocol handler factory object */
  136. var JSDProtocolHandlerFactory = new Object();
  137.  
  138. JSDProtocolHandlerFactory.createInstance =
  139. function jsdhf_create (outer, iid) {
  140.     if (outer != null)
  141.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  142.  
  143.     if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
  144.         throw Components.results.NS_ERROR_INVALID_ARG;
  145.  
  146.     return new JSDProtocolHandler();
  147. }
  148.  
  149. function JSDURI (spec, charset)
  150. {
  151.     this.spec = this.prePath = spec;
  152.     this.charset = this.originCharset = charset;
  153. }
  154.  
  155. JSDURI.prototype.QueryInterface =
  156. function jsdch_qi (iid)
  157. {
  158.     if (!iid.equals(nsIURI) && !iid.equals(nsIURL) &&
  159.         !iid.equals(nsISupports))
  160.         throw Components.results.NS_ERROR_NO_INTERFACE;
  161.  
  162.     return this;
  163. }
  164.  
  165. JSDURI.prototype.scheme = "x-jsd";
  166.  
  167. JSDURI.prototype.fileBaseName =
  168. JSDURI.prototype.fileExtension =
  169. JSDURI.prototype.filePath  =
  170. JSDURI.prototype.param     =
  171. JSDURI.prototype.query     =
  172. JSDURI.prototype.ref       =
  173. JSDURI.prototype.directory =
  174. JSDURI.prototype.fileName  =
  175. JSDURI.prototype.username  =
  176. JSDURI.prototype.password  =
  177. JSDURI.prototype.hostPort  =
  178. JSDURI.prototype.path      =
  179. JSDURI.prototype.asciiHost =
  180. JSDURI.prototype.userPass  = "";
  181.  
  182. JSDURI.prototype.port = JSD_DEFAULT_PORT;
  183.  
  184. JSDURI.prototype.schemeIs =
  185. function jsduri_schemeis (scheme)
  186. {
  187.     return scheme.toLowerCase() == "x-jsd";
  188. }
  189.  
  190. JSDURI.prototype.getCommonBaseSpec =
  191. function jsduri_commonbase (uri)
  192. {
  193.     return "x-jsd:";
  194. }
  195.  
  196. JSDURI.prototype.getRelativeSpec =
  197. function jsduri_commonbase (uri)
  198. {
  199.     return uri;
  200. }
  201.  
  202. JSDURI.prototype.equals =
  203. function jsduri_equals (uri)
  204. {
  205.     return uri.spec == this.spec;
  206. }
  207.  
  208. JSDURI.prototype.clone =
  209. function jsduri_clone ()
  210. {
  211.     return new JSDURI (this.spec);
  212. }
  213.  
  214. JSDURI.prototype.resolve =
  215. function jsduri_resolve(path)
  216. {
  217.     //dump ("resolve " + path + " from " + this.spec + "\n");
  218.     if (path[0] == "#")
  219.         return this.spec + path;
  220.     
  221.     return path;
  222. }
  223.  
  224. function JSDProtocolHandler()
  225. {
  226.     /* nothing here */
  227. }
  228.  
  229. JSDProtocolHandler.prototype.scheme = "x-jsd";
  230. JSDProtocolHandler.prototype.defaultPort = JSD_DEFAULT_PORT;
  231. JSDProtocolHandler.prototype.protocolFlags = nsIProtocolHandler.URI_NORELATIVE ||
  232.                                              nsIProtocolHandler.URI_NOAUTH;
  233.  
  234. JSDProtocolHandler.prototype.allowPort =
  235. function jsdph_allowport (aPort, aScheme)
  236. {
  237.     return false;
  238. }
  239.  
  240. JSDProtocolHandler.prototype.newURI =
  241. function jsdph_newuri (spec, charset, baseURI)
  242. {
  243.     var clazz = Components.classes[SIMPLEURI_CTRID];
  244.     var uri = clazz.createInstance(nsIURI);
  245.     uri.spec = spec;
  246.     return uri;
  247. }
  248.  
  249. JSDProtocolHandler.prototype.newChannel =
  250. function jsdph_newchannel (uri)
  251. {
  252.     return new JSDChannel (uri);
  253. }
  254.  
  255. function JSDChannel (uri)
  256. {
  257.     this.URI = uri;
  258.     this.originalURI = uri;
  259.     this._isPending = true;
  260.     var clazz = Components.classes[STRING_STREAM_CTRID];
  261.     this.stringStream = clazz.createInstance(nsIStringInputStream);
  262. }
  263.  
  264. JSDChannel.prototype.QueryInterface =
  265. function jsdch_qi (iid)
  266. {
  267.  
  268.     if (!iid.equals(nsIChannel) && !iid.equals(nsIRequest) &&
  269.         !iid.equals(nsISupports))
  270.         throw Components.results.NS_ERROR_NO_INTERFACE;
  271.  
  272.     return this;
  273. }
  274.  
  275. /* nsIChannel */
  276. JSDChannel.prototype.loadAttributes = null;
  277. JSDChannel.prototype.contentType = "text/html";
  278. JSDChannel.prototype.contentLength = -1;
  279. JSDChannel.prototype.owner = null;
  280. JSDChannel.prototype.loadGroup = null;
  281. JSDChannel.prototype.notificationCallbacks = null;
  282. JSDChannel.prototype.securityInfo = null;
  283.  
  284. JSDChannel.prototype.open =
  285. function jsdch_open()
  286. {
  287.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  288. }
  289.  
  290. JSDChannel.prototype.asyncOpen =
  291. function jsdch_aopen (streamListener, context)
  292. {
  293.     this.streamListener = streamListener;
  294.     this.context = context;
  295.     
  296.     if (!window && this.URI.spec == "x-jsd:debugger")
  297.     {
  298.         this.contentType = "x-application-jsd";
  299.         this.contentLength = 0;
  300.         streamListener.onStartRequest(this, context);
  301.         return;
  302.     }
  303.     
  304.     var window = findDebuggerWindow();
  305.     var ary = this.URI.spec.match (/x-jsd:([^:]+)/);
  306.     var exception;
  307.  
  308.     if (this.loadGroup)
  309.         this.loadGroup.addRequest (this, null);
  310.  
  311.     if (window && "console" in window && ary)
  312.     {
  313.         try
  314.         {
  315.             window.asyncOpenJSDURL (this, streamListener, context);
  316.             return;
  317.         }
  318.         catch (ex)
  319.         {
  320.             exception = ex;
  321.         }
  322.     }
  323.  
  324.     var str =
  325.         "<html><head><title>Error</title></head><body>Could not load <<b>" +
  326.         safeHTML(this.URI.spec) + "</b>><br>";
  327.     
  328.     if (!ary)
  329.     {
  330.         str += "<b>Error parsing uri.</b>";
  331.     }
  332.     else if (exception)
  333.     {
  334.         str += "<b>Internal error: " + safeHTML(exception) + "</b><br><pre>" + 
  335.             safeHTML(exception.stack);
  336.     }
  337.     else
  338.     {
  339.         str += "<b>Debugger is not running.</b>";
  340.     }
  341.     
  342.     str += "</body></html>";
  343.     
  344.     this.respond (str);
  345. }
  346.  
  347. JSDChannel.prototype.respond =
  348. function jsdch_respond (str)
  349. {
  350.     this.streamListener.onStartRequest (this, this.context);
  351.  
  352.     var len = str.length;
  353.     this.stringStream.setData (str, len);
  354.     this.streamListener.onDataAvailable (this, this.context,
  355.                                          this.stringStream, 0, len);
  356.     this.streamListener.onStopRequest (this, this.context,
  357.                                        Components.results.NS_OK);
  358.     if (this.loadGroup)
  359.         this.loadGroup.removeRequest (this, null, Components.results.NS_OK);
  360.     this._isPending = false;    
  361. }
  362.  
  363. /* nsIRequest */
  364. JSDChannel.prototype.isPending =
  365. function jsdch_ispending ()
  366. {
  367.     return this._isPending;
  368. }
  369.  
  370. JSDChannel.prototype.status = Components.results.NS_OK;
  371.  
  372. JSDChannel.prototype.cancel =
  373. function jsdch_cancel (status)
  374. {
  375.     if (this._isPending)
  376.     {
  377.         this._isPending = false;
  378.         this.streamListener.onStopRequest (this, this.context, status);
  379.         if (this.loadGroup)
  380.         {
  381.             try
  382.             {
  383.                 this.loadGroup.removeRequest (this, null, status);
  384.             }
  385.             catch (ex)
  386.             {
  387.                 debug ("we're not in the load group?\n");
  388.             }
  389.         }
  390.     }
  391.     
  392.     this.status = status;
  393. }
  394.  
  395. JSDChannel.prototype.suspend =
  396. JSDChannel.prototype.resume =
  397. function jsdch_notimpl ()
  398. {
  399.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  400. }
  401.  
  402. /*****************************************************************************/
  403.  
  404. /* x-application-jsd content handler */
  405. function JSDContentHandler ()
  406. {}
  407.  
  408. JSDContentHandler.prototype.QueryInterface =
  409. function jsdh_qi(iid)
  410. {
  411.     if (!iid.equals(nsIContentHandler) && !iid.equals(nsISupports))
  412.         throw Components.results.NS_ERROR_NO_INTERFACE;
  413.  
  414.     return this;
  415. }
  416.  
  417. JSDContentHandler.prototype.handleContent =
  418. function jsdh_handle(contentType, command, windowTarget, request)
  419. {
  420.     var e;
  421.     var channel = request.QueryInterface(nsIChannel);
  422.     
  423.     // prevent someone from invoking the debugger remotely by serving
  424.     // up any old file with the x-application-jsd content type.
  425.     if (channel.URI.spec != "x-jsd:debugger")
  426.     {
  427.         debug ("Not handling content from unknown location ``" +
  428.                channel.URI.spec + "''");
  429.         return;
  430.     }
  431.     
  432.     var window = findDebuggerWindow()
  433.  
  434.     if (window)
  435.     {
  436.         window.focus();
  437.     }
  438.     else
  439.     {
  440.         var ass =
  441.             Components.classes[ASS_CONTRACTID].getService(nsIAppShellService);
  442.         window = ass.hiddenDOMWindow;
  443.  
  444.         var args = new Object();
  445.         args.url = channel.URI.spec;
  446.  
  447.         window.openDialog("chrome://venkman/content/venkman.xul", "_blank",
  448.                           "chrome,menubar,toolbar,resizable,dialog=no", args);
  449.     }
  450. }
  451.  
  452. /*****************************************************************************/
  453.  
  454. /* content handler factory object (IRCContentHandler) */
  455. var JSDContentHandlerFactory = new Object();
  456.  
  457. JSDContentHandlerFactory.createInstance =
  458. function jsdhf_create(outer, iid)
  459. {
  460.     if (outer != null)
  461.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  462.  
  463.     if (!iid.equals(nsIContentHandler) && !iid.equals(nsISupports))
  464.         throw Components.results.NS_ERROR_INVALID_ARG;
  465.  
  466.     return new JSDContentHandler();
  467. }
  468.  
  469. /*****************************************************************************/
  470.  
  471. var Module = new Object();
  472.  
  473. Module.registerSelf =
  474. function (compMgr, fileSpec, location, type)
  475. {
  476.     debug("*** Registering -venkman handler.\n");
  477.     
  478.     compMgr =
  479.         compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  480.  
  481.     compMgr.registerFactoryLocation(CLINE_SERVICE_CID,
  482.                                     "Venkman CommandLine Service",
  483.                                     CLINE_SERVICE_CTRID, 
  484.                                     fileSpec,
  485.                                     location, 
  486.                                     type);
  487.  
  488.     catman = Components.classes[CATMAN_CTRID].getService(nsICategoryManager);
  489.     catman.addCategoryEntry("command-line-argument-handlers",
  490.                             "venkman command line handler",
  491.                             CLINE_SERVICE_CTRID, true, true);
  492.  
  493.     debug("*** Registering x-jsd protocol handler.\n");
  494.     compMgr.registerFactoryLocation(JSDPROT_HANDLER_CID,
  495.                                     "x-jsd protocol handler",
  496.                                     JSDPROT_HANDLER_CTRID, 
  497.                                     fileSpec, 
  498.                                     location,
  499.                                     type);
  500.  
  501.     debug("*** Registering x-application-jsd content handler.\n");
  502.     compMgr.registerFactoryLocation(JSDCNT_HANDLER_CID,
  503.                                     "x-application-jsd content handler",
  504.                                     JSDCNT_HANDLER_CTRID, 
  505.                                     fileSpec, 
  506.                                     location,
  507.                                     type);
  508.     try
  509.     {
  510.         const JSD_CTRID = "@mozilla.org/js/jsd/debugger-service;1";
  511.         const jsdIDebuggerService = Components.interfaces.jsdIDebuggerService;
  512.         var jsds = Components.classes[JSD_CTRID].getService(jsdIDebuggerService);
  513.         jsds.initAtStartup = true;
  514.     }
  515.     catch (ex)
  516.     {
  517.         debug ("*** ERROR initializing debugger service");
  518.         debug (ex);
  519.     }
  520. }
  521.  
  522. Module.unregisterSelf =
  523. function(compMgr, fileSpec, location)
  524. {
  525.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  526.  
  527.     compMgr.unregisterFactoryLocation(CLINE_SERVICE_CID, fileSpec);
  528.     catman = Components.classes[CATMAN_CTRID].getService(nsICategoryManager);
  529.     catman.deleteCategoryEntry("command-line-argument-handlers",
  530.                                CLINE_SERVICE_CTRID, true);
  531. }
  532.  
  533. Module.getClassObject =
  534. function (compMgr, cid, iid) {
  535.     if (cid.equals(CLINE_SERVICE_CID))
  536.         return CLineFactory;
  537.  
  538.     if (cid.equals(JSDPROT_HANDLER_CID))
  539.         return JSDProtocolHandlerFactory;
  540.  
  541.     if (cid.equals(JSDCNT_HANDLER_CID))
  542.         return JSDContentHandlerFactory;
  543.     
  544.     if (!iid.equals(Components.interfaces.nsIFactory))
  545.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  546.  
  547.     throw Components.results.NS_ERROR_NO_INTERFACE;
  548.     
  549. }
  550.  
  551. Module.canUnload =
  552. function(compMgr)
  553. {
  554.     return true;
  555. }
  556.  
  557. /* entrypoint */
  558. function NSGetModule(compMgr, fileSpec) {
  559.     return Module;
  560. }
  561.